package kuang.juc.completabletfuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ////没有返回值的runAsync方法
        //CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
        //    try {
        //        TimeUnit.SECONDS.sleep(2);
        //    } catch (InterruptedException e) {
        //        e.printStackTrace();
        //    }
        //    System.out.println(Thread.currentThread().getName());
        //});
        //
        //System.out.println("main");
        //completableFuture.get();

        //有返回值的亦不会掉supplyAsync()
        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName());
            int i= 10/0;
            return 1024;
        }).whenComplete((t,u)->{    //这个方法可以获取详细的信息，正确的结果和报错信息
            System.out.println("t = " + t); //正常结果的信息
            System.out.println("u = " + u); //报错的信息
        }).exceptionally((e)->{
            //System.out.println(e.getMessage());
            return 233;
        });

        System.out.println(completableFuture.get());
    }
}
